/*
/*
 * queue.c
 *
 *  Created on: May 23, 2013
 *      Author: Aden
 */


#include <stdio.h>
#include <stdlib.h>
#include "global.h"
#include "queue.h"


/*
 * Queue Constructor
 */
QueuePtr newQueueConstructor(void)
{
	QueuePtr queue  = (QueuePtr) malloc(sizeof(QueueStr));
	queue->Queue = (int*) malloc (sizeof(int) * 1000);
	queue->size = 0;
	queue->back = -1;
	queue->front = 0;

	//pointers to functions
	queue->dequeue = dequeue;
	queue->enqueue = enqueue;
	queue->empty = empty;
	queue->display = display;
	queue->getQueue = getQueue;

	return queue;
}
/**
 * Insert a value at the end of the queue
 * this - the QueuePte
 * value - the value
 */
int enqueue(QueuePtr this, int value)
{
	if(this->Queue == NULL)
	{
		return NULL_POINTER_ERROR;
	}
	else if(this->back == 1000)
	{
		printw("\nOVERFLOW!!!");
	}
	else
	{

		this->back =(this->back + 1) % MAX_QUEUE_SIZE;
		this->Queue[this->back] = value;
		this->size++;
	}

	return NO_ERROR;
}
/*
 * Return the value in front of the queue
 * this - the QueuePtr
 */
int dequeue(QueuePtr this)
{
	int value = this->Queue[this->front];
	if(this->size == 0)
	{
			}
	else
	{
		this->front=(this->front + 1) % MAX_QUEUE_SIZE;
		this->size--;
	}
	return value;
}
/**
 * Return empty if the queue is or otherwise
 * this - the QueuePtr
 *
 */
int empty(QueuePtr this)
{
	return this->size == 0;
}
/*
 * Return the array of queue
 * this - the QueuePtr
 */
int* getQueue(QueuePtr this)
{
	int i;
	int * temp = (int*) malloc(sizeof(int) * 1000);
	if(this->size == 0)
	{
		printw("\nQueue is Empty!!!");
	}
	else
	{
		for(i = this->front;  i <= this->back; i++)
		{
			temp[i] = this->Queue[i];
		}
	}
	return temp;
}
/*
 * Queue object representation
 * this - the QueuePtr
 */
int display(QueuePtr this)
{
	int i;
	printw("Q: %d[ ", this->size);
	for(i = this->front;  i <= this->back; i++)
	{
		printw("%d ",this->Queue[i]);
	}
	printw("]\n");
	return NO_ERROR;
}
//int main(void)
//{
//
//	QueuePtr q = newQueueConstructor();
//	q->enqueue(q, 1);
//	q->enqueue(q, 2);
//	q->enqueue(q, 3);
//	q->enqueue(q, 4);
//	int x = q->dequeue(q);
//
//    printw("B:%d\n", q->Queue[q->back]);
//	printw("Dequeue:%d\n", x);
//	q->display(q);
//
//
//	return 0;
//}
